package com.wsdcd.zlg.aspect;

import com.wsdcd.zlg.model.entity.User;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
@Aspect
public class insertAspect {

        @Pointcut("execution(public * com.wsdcd.zlg.model.service..*.insert(..))")
        public void insert() {

        }
//        @Before("insert()")
//        public void beforeMethod(JoinPoint joinPoint) {
//            System.out.println(" execute start");
//        }
//        @After("insert()")
//        public void afterMethod() {
//            System.out.println(" execute end");
//        }
        // 环绕,之前和之后都有,相当于 @Before 和 @After 一起使用
        @Around("insert()")
        public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
            // @Around也可以和@Before 和 @After 一起使用
            User user = (User) pjp.getArgs()[0];
            user.setCreateBy("曾莅根");
            long times = System.currentTimeMillis();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time = sdf.format(times);
            user.setCreateTime(time);
//            System.out.println(user);
//            System.out.println("around start");
            // 调用被代理的方法
            pjp.proceed();
//            System.out.println("around end");
        }
}
